package grib2handler.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;

/**
 * <p>
 * Title: PropertiesUtil
 * </p>
 * <p>
 * Description: 配置文件读取
 * </p>
 * <p>
 * Copyright: Copyright (c) 2014
 * </p>
 * 
 * @author charlie
 * @version 1.0.0
 */
public class PropertiesUtil {
	/** 日志打印 */
	private static Logger logger = Logger.getLogger(PropertiesUtil.class);
	private static CompositeConfiguration config = new CompositeConfiguration();
	/**
	 * 匹配正则
	 */
	private static List<String> match = new ArrayList<String>();
	private static Map<String, HashSet<String>> userPath = new HashMap<String, HashSet<String>>();
	/**
	 * 匹配值
	 */
	private static Map<String, String> matchValue = new HashMap<String, String>();
	/**
	 * 是否保存其他数据
	 */
	private static boolean saveOther = true;
	
	static {
		try {
			String path = "conf/application.properties";
			File f = new File(path);
			if (f.exists()) {
				config.addConfiguration(new PropertiesConfiguration(path));
				saveOther = config.getBoolean("saveOther", true);
				Iterator<String> keys = config.getKeys();
				 while (keys.hasNext()) {  
		            String key = keys.next();  
		            if(key.startsWith("bufr.")) {
		            	String[] value = config.getString(key).split(":");
		            	match.add(value[0]);
		            	matchValue.put(value[0], value[1]);
		            } else if(key.startsWith("user.")) {
		            	String[] value = config.getString(key).split(";");
		            	String[] model = value[1].split(":");
		            	HashSet<String> set = new HashSet<String>();
		            	for (String string : model) {
							set.add(string);
						}
		            	userPath.put(value[0], set);
		            }
			     }  
			}
		} catch (Exception e) {
			logger.error("配置解析失败" + e.getMessage());
		}
	}
	
	public static CompositeConfiguration getConfig() {
		return config;
	}
	
	public static List<String> getMatch() {
		return match;
	}
	public static Map<String, String> getMatchValue() {
		return matchValue;
	}
	public static boolean isSaveOther() {
		return saveOther;
	}

	public static Map<String, HashSet<String>> getUserPath() {
		return userPath;
	}
	
}
